<style scoped>

</style>
<template>
    <div class="create-inquiry-sheet-page">
        <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <el-form-item label="法人名称" prop="legalPersonName"  :rules="[{ required: true, message: '法人名称不能为空'}]"  v-if="!powerFlag">
                <el-input size="mini" v-if="legalPersonDropDown.length==1" disabled v-model.trim="formInline.legalPersonName"></el-input>
                <el-select size="mini" v-else  v-model.trim="formInline.legalPersonName" placeholder="请选择" @change="changeLegalPerson">
                    <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.legalPersonName" :value="legalPerson.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报价单号" >
                <el-input size="mini" disabled v-model="formInline.quotationNumber"></el-input>
            </el-form-item>
            <el-form-item label="报价日期" >
                <el-input size="mini" disabled v-model="formInline.createTime"></el-input>
            </el-form-item>
            <el-form-item label="报价人员" >
                <el-input size="mini" disabled v-model="formInline.bidderName"></el-input>
            </el-form-item>
            <!-- 
            <el-form-item label="报价单位" v-if="formInline.vendorId && formInline.selectDropdown.length==1" prop="vendor">
                <el-input size="mini" disabled v-model="formInline.vendor"></el-input>
            </el-form-item>
            -->
            <el-form-item label="报价单位" v-if="powerFlag" prop="vendor">
                <el-input size="mini" disabled v-model="formInline.vendor"></el-input>
            </el-form-item>
            <el-form-item label="报价单位" v-else prop="vendorCode"  :rules="[{ required: true, message: '报价单位不能为空'}]" >
                <el-select size="mini" v-model.trim="formInline.vendorCode" placeholder="请选择" @change="changeSupper">
                    <el-option v-for="(vendor,index) in formInline.selectDropdown" :key="index" :label="vendor.partnerName" :value="vendor.partnerCode"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报价对象" v-if="powerFlag && qObjDropDownValue.length==1" >
                <el-input size="mini" disabled v-model="formInline.legalPersonName"></el-input>
            </el-form-item>
             <el-form-item label="报价对象" v-else-if="powerFlag" prop="legalPersonCode"  :rules="[{ required: true, message: '报价对象不能为空'}]" >
                <el-select size="mini" v-model.trim="formInline.legalPersonCode" placeholder="请选择" @change="selectedQObj">
                    <el-option v-for="(qObj,index) in qObjDropDownValue" :key="index" :label="qObj.legalPersonName" :value="qObj.legalPersonCode"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报价类型" prop="biddingType" :rules="[{ required: true, message: '报价类型为必填项'}]" >
                <el-select size="mini" v-model.trim="formInline.biddingType" placeholder="请选择">
                    <el-option v-for="(biddingType,index) in selectDropdown.type" :key="index" :label="biddingType.label" :value="biddingType.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报价币别" prop="currencyCode"  :rules="[{ required: true, message: '报价币别不能为空'}]" >
                <el-select size="mini" v-model.trim="formInline.currencyCode" placeholder="请选择" @change="selectCurrencyCode">
                    <el-option v-for="(inquiryCurrency,index) in selectDropdown.currencyCode" :key="index" :label="inquiryCurrency.label" :value="inquiryCurrency.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item prop="exchangeRate" label="汇率" :rules="[{ required: true, message: '汇率不能为空'}]" >
                <el-input size="mini"  v-model.number="formInline.exchangeRate" placeholder="请输入汇率"></el-input>
            </el-form-item>
            <el-form-item label="金额是否含税"  prop="isTax"  :rules="[{ required: true, message: '此项为必填项'}]">
                <el-select size="mini" v-model.trim="formInline.isTax" placeholder="请选择" @change="updateTax">
                    <el-option v-for="(isTax,index) in selectDropdown.isTax" :key="index" :label="isTax.label" :value="isTax.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报价生效日期" prop="startTime" :rules="[{ required: true, message: '报价生效日期不能为空'}]">
                <el-date-picker type="date" size="mini" :editable="false" :clearable="false" v-model.trim="formInline.startTime" placeholder="选择报价生效日期" @change="selectedStartTime" :picker-options="pickerOptions"></el-date-picker>
            </el-form-item>
            <el-form-item label="报价失效日期" prop="endTime" :rules="[{ required: true, message: '报价失效日期不能为空'}]">
                <el-date-picker type="date" size="mini" :editable="false" :clearable="false" v-model.trim="formInline.endTime"  placeholder="选择报价失效日期" @change="selectedEndTime" :picker-options="pickerOptions"></el-date-picker>
            </el-form-item>
            <el-form-item label="报价总数量">
                <el-input size="mini" disabled placeholder="" v-model="formInline.totalQuantityQuoted"></el-input>
            </el-form-item>
            <el-form-item label="报价不含税总金额">
                <el-input size="mini" disabled placeholder="" v-model="formInline.totalQuotedPrice"></el-input>
            </el-form-item>
            <el-form-item label="报价含税总金额">
                <el-input size="mini" disabled placeholder="" v-model="formInline.totalQuotedTaxPrice"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button class="bolder outsideBtnBS selectAll" size="mini" type="danger" @click="save(10)">保存</el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" v-if="formInline.biddingStatus==20" disabled>提交</el-button>
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" v-else @click="onSubmit(20)">提交</el-button>
            </el-form-item>
        </el-form>
        <label class="default-label">待报价明细：</label>
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="leadingInMask">导入</el-button>
        <el-table :data="biddingDetailsData" border style="width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中">
            <el-table-column type="index"  header-align="center" align="center" label="序"  width="40"></el-table-column>
            <el-table-column prop="spareCoding" label="备件编码" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="spareName" label="备件名称" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="measurementUnit" label="计量单位" header-align="center" width="100"  show-overflow-tooltip></el-table-column>
            <el-table-column prop="inquiryQty" label="报价数量" header-align="center" width="100" show-overflow-tooltip>
                <!--<template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.quotedQuantity" placeholder=""  @change="InputInquiryQty(scope.$index, scope.row,$event)"></el-input>
                </template>-->
            </el-table-column>
            <el-table-column prop="currencyCodeWord" label="币别" header-align="center" width="80"  show-overflow-tooltip></el-table-column>
            <!--含税单价金额-->
            <el-table-column prop="includingTaxPrice" label="含税单价金额" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.includingTaxPrice" :disabled="formInline.isTax!=1" @change="inputTaxPrice(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <!--不含税单价金额 -->
            <el-table-column prop="quotationAmount" label="不含税单价金额" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.quotationAmount" :disabled="formInline.isTax!=2" @change="inputQuotationAmount(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <!--新增税率字段-->
            <el-table-column  label="税率(%)" header-align="center" width="150" prop="taxRate" show-overflow-tooltip>
                <template scope="scope">
                    <el-select size="mini" clearable  v-model="scope.row.taxRate" placeholder="请选择" @change="inputTaxRate(scope.$index, scope.row,$event)" v-if="!taxDisabled">
                        <el-option v-for="(taxR,index) in taxData" :key="index" :label="taxR.tax" :value="taxR.tax">
                        </el-option>
                    </el-select>
                    <el-input size="mini"  v-model="scope.row.taxRate" disabled v-else></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="quantityAvailable" label="可供数量" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.quantityAvailable" placeholder=""  @change="inputQuantityAvailable(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="moq" label="MOQ" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.moq" placeholder=""  @change="inputMOQ(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="deliveryCycle" label="交货周期(天)" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.deliveryCycle" placeholder=""  @change="inputDeliveryCycle(scope.$index, scope.row,$event)"></el-input>
                </template>
            </el-table-column>
            <el-table-column prop="quotationExplain" label="说明" header-align="center" width="100" show-overflow-tooltip>
                <template scope="scope">
                    <el-input size="mini"  v-model.number="scope.row.quotationExplain" placeholder=""></el-input>
                </template>
            </el-table-column>
        </el-table>
        <!--导入的弹框-->
        <el-dialog title="导入备件信息" id="LeadIngInMark" :visible.sync="leadIngInVisible" size="tiny" :lock-scroll="false">
            <el-upload class="upload-demo"  :action="action" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" :on-preview="handlePreview" :on-remove="handleRemove" :file-list="fileList" :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess">
                <el-button size="mini" type="danger">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
            </el-upload>
        </el-dialog>
    </div>
</template>


<script>

    export default {
        data() {
            return {
                pickerOptions: {     //报价生效日期/结束日期不得早于当天
                    disabledDate(time) {
                        return time.getTime() < Date.now() - 8.64e7;
                    }
                },
                // urlName:"",   //当前地址信息
                // 查询表单
                formInline:{
                    id:"",
                    exchangeRate:"",
                    totalQuantityQuoted:"",
                    totalQuotedPrice:"",
                    totalQuotedTaxPrice:"",
                    legalPersonCode:"",
                    legalPersonName:"",
                    requestForQuotationId:"",
                    quotationNum:"",
                    createTime:"",
                    bidder:"",
                    bidderName:"",
                    legalPersonId:"",
                    biddingType:"",
                    currencyCode:"",
                    currencyCodeWord:"",
                    isTax:"",
                    isTaxName:"",
                    startTime:"",
                    endTime:"",
                    vendor:"",
                    vendorId:"",
                    vendorCode:"",
                    spareCodes:"",
                    quantityAvailable:"",
                    legalPersonNameList:[],
                    //  供应商下拉列表
                    selectDropdown:[]  // 下拉列表的值
                },
                legalPersonDropDown:[],
                loading:false,
                biddingDetailsData: [],// 待报价明细表格
                // 报价类型、是否含税、币别类型下拉列表
                selectDropdown:[],  // 下拉列表的值
                qObjDropDownValue:[],    //报价对象下拉列表
                spareData:[],    // 选择备件弹窗
                taxData:[],//税率列表
                formLabelWidth:"90",
                //选择备件弹窗的 分页
                sparePageIndex:1,    //当前页（必传）
                sparePageSize :5,   //每页多少条
                spareTotalNumber: 20,     //总条数
                spareTotalPage:2,    //总页数
                supplierPageIndex:1,    //当前页（必传）
                supplierPageSize :5,
                supplierTotalNumber: 20,     //总条数
                supplierTotalPage:2,
                leadIngInVisible:false,   //导入弹窗
                action:"",
                fileList:[],
                taxDisabled:false,
                //权限
                powerFlag:false,   //不是业务合作人员
                
                //查询禁用
                selectDisabled:false,
                saveDisabled:false,  // 保存禁用
                submitDisabled:false  // 提交禁用
            };
        },
        computed:{
            identity(){
                return this.$store.state.identity;
            }
        },
        watch:{
            identity(){
                var _this=this;
                if(_this.identity == 2){
                    _this.powerFlag = true;   //是业务合作人员
                    _this.getVendorCode();  //获取当前登录用户对应的供应商
                }else if(_this.identity == 1){
                    _this.legalPersonDropDownValue();
                }
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'CreateQuotation');
            //  //ajax前后查询按钮禁用(保存、提交)
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            //     _this.saveDisabled=true;
            //     _this.submitDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            //     _this.saveDisabled=false;
            //     _this.submitDisabled=false;
            // });

            //下来列表的值
            _this.createInquerySheetDropdownValue();
            
             _this.formInline.quotationNumber=_this.$route.query.quotationNumber;
            // 上传地址
            _this.action=_this.procurement_service+"quotation/QuotationIntroduction";
            _this.formInline.vendorCode=_this.$route.query.vendorCode;
            _this.formInline.spareCodes=_this.$route.query.spareCodes;
            _this.formInline.requestForQuotationId=_this.$route.query.requestForQuotationId;
            if(_this.identity == 2){
                _this.powerFlag = true;   //是业务合作人员
                _this.getVendorCode();  //获取当前登录用户对应的供应商
            }else if(_this.identity == 1){
                _this.legalPersonDropDownValue();
            }

            _this.seeInquiry();  //进入页面直接 查看报价单列表
            _this.getUser();
            _this.getTaxData();//获取税率
        },
        methods: {
            //获得指定人姓名 bidderName
            getUser(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"/plan/getUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.formInline.bidderName = data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            }, 
            // 获取当前登录用户对应的供应商
            getVendorCode(){     //报价单位
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"quotation/findByVendorName",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.formInline.vendorId = data.data[0].id;
                            _this.formInline.vendorCode = data.data[0].partnerCode;
                            _this.formInline.vendor = data.data[0].partnerName;
                            _this.quotationObj();//报价对象下拉列表
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 报价对象下拉列表
            quotationObj(){ 
                var _this = this;
                var tmp = {id:_this.formInline.vendorId};
                $.ajax({
                    url:_this.metadata_service+'partner/queryLegalPerson',
                    type:'POST',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:'application/json;charset=utf-8',
                    success:function(data){
                        if(data.success){
                            _this.qObjDropDownValue = data.data;
                            if(_this.qObjDropDownValue && _this.qObjDropDownValue.length==1){
                                _this.formInline.legalPersonId = _this.qObjDropDownValue[0].id;
                                _this.formInline.legalPersonCode = _this.qObjDropDownValue[0].legalPersonCode;
                                _this.formInline.legalPersonName = _this.qObjDropDownValue[0].legalPersonName;
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 选择报价对象
            selectedQObj(){
                var _this = this;
                for(var i=0;i<_this.qObjDropDownValue.length;i++){
                    if(_this.formInline.legalPersonCode == _this.qObjDropDownValue[i].legalPersonCode){
                        _this.formInline.legalPersonId = _this.qObjDropDownValue[i].id;
                        _this.formInline.legalPersonName = _this.qObjDropDownValue[i].legalPersonName;
                    }
                }
            },
            //计算报价总数量
            totalQuantity(){
                var _this=this;
                _this.formInline.totalQuantityQuoted = 0;
                if(_this.biddingDetailsData!= null && _this.biddingDetailsData.length){
                    setTimeout(function(){
                        for(var i=0;i<_this.biddingDetailsData.length;i++){
                            _this.biddingDetailsData[i].inquiryQty = Number(_this.biddingDetailsData[i].inquiryQty);
                            _this.formInline.totalQuantityQuoted +=_this.biddingDetailsData[i].inquiryQty;
                        }
                    },0);
                }
            },
            //获取税率列表
            getTaxData(){
                var _this=this;
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"tax/findTaxDuplicateRemovalList",
                    cache: false,
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",     //*****
                    success: function (data) {
                        if (data.success) {
                            _this.taxData=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                });
            },
            //报价生效日期
            selectedStartTime(val){
                var _this=this;
                _this.formInline.startTime = val;
                if(_this.formInline.endTime && val > _this.formInline.endTime){
                    _this.common.errorTip("报价生效日期不能大于报价失效日期");
                    _this.formInline.startTime = "";
                };
            },
            // 报价失效日期
            selectedEndTime(val){
                var _this=this;
                _this.formInline.endTime = val;
                if(_this.formInline.startTime && val < _this.formInline.startTime){
                    _this.common.errorTip("报价失效日期不能小于报价生效日期");
                    _this.formInline.endTime = "";
                };
            },
            // 新增的接口  新增后保存/提交
            addData(biddingStatus){
                var _this = this;  
                if(_this.biddingDetailsData && _this.biddingDetailsData.length){
                    for(var i=0;i<_this.biddingDetailsData.length;i++){
                        _this.biddingDetailsData[i].quotedQuantity =_this.biddingDetailsData[i].inquiryQty;
                    }
                    var tmp = {
                        legalPersonName:_this.formInline.legalPersonName,
                        vendorCode:_this.formInline.vendorCode,
                        vendor:_this.formInline.vendor,
                        totalQuantityQuoted:_this.formInline.totalQuantityQuoted,
                        totalQuotedPrice:_this.formInline.totalQuotedPrice,
                        totalQuotedTaxPrice:_this.formInline.totalQuotedTaxPrice,
                        exchangeRate:_this.formInline.exchangeRate,
                        vendorId:_this.formInline.vendorId,
                        spareId:_this.biddingDetailsData.id,
                        quantityAvailable:_this.quantityAvailable,
                        currencyCode:_this.formInline.currencyCode,
                        quotationNumber:_this.formInline.quotationNumber,
                        biddingStatus:biddingStatus,
                        createTime:_this.formInline.createTime,
                        bidder:_this.formInline.bidder,
                        legalPersonId:_this.formInline.legalPersonId,
                        biddingType:_this.formInline.biddingType,
                        isTax:_this.formInline.isTax,
                        startTime:_this.formInline.startTime,
                        endTime:_this.formInline.endTime,
                        quotationItem:_this.biddingDetailsData
                        };
                    $.ajax({
                        url:_this.procurement_service+'quotation/add',
                        type:'POST',
                        cache:false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType:'application/json;charset=utf-8',
                        success:function(data){
                            if(data.success){
                                _this.common.successTip("操作成功！");
                                //_this.formInline=data.data;
                                _this.formInline.quotationNumber=data.data.quotationNumber;   //报价单号
                                _this.formInline.createTime=data.data.createTime;   //报价日期
                                //_this.biddingDetailsData=data.data.quotationItem;
                                if(biddingStatus==20){
                                    _this.$router.push("/QuotationSheet");
                                }
                            }else{
                                _this.common.errorTip(data.errorMessage);
                            }
                        }
                    })
                }else{
                    _this.common.errorTip("待报价明细不能为空！");
                    return;
                }
               
            },

            // 修改的接口  修改后保存/提交
            updateData(biddingStatus){
                var _this = this;
                if(_this.biddingDetailsData && _this.biddingDetailsData.length){
                    for(var i=0;i<_this.biddingDetailsData.length;i++){
                        _this.biddingDetailsData[i].quotedQuantity =_this.biddingDetailsData[i].inquiryQty;
                    }
                    var tmp = {
                        legalPersonName:_this.formInline.legalPersonName,
                        vendorCode:_this.formInline.vendorCode,
                        vendor:_this.formInline.vendor,
                        totalQuantityQuoted:_this.formInline.totalQuantityQuoted,
                        totalQuotedPrice:_this.formInline.totalQuotedPrice,
                        totalQuotedTaxPrice:_this.formInline.totalQuotedTaxPrice,
                        exchangeRate:_this.formInline.exchangeRate,
                        vendorId:_this.formInline.vendorId,
                        spareId:_this.biddingDetailsData.id,
                        quantityAvailable:_this.quantityAvailable,
                        currencyCode:_this.formInline.currencyCode,
                        quotationNumber:_this.formInline.quotationNumber,
                        biddingStatus:biddingStatus,
                        createTime:_this.formInline.createTime,
                        bidder:_this.formInline.bidder,
                        legalPersonId:_this.formInline.legalPersonId,
                        biddingType:_this.formInline.biddingType,
                        isTax:_this.formInline.isTax,
                        startTime:_this.formInline.startTime,
                        endTime:_this.formInline.endTime,
                        quotationItem:_this.biddingDetailsData};
                    $.ajax({
                        url:_this.procurement_service+'quotation/update',
                        type:'POST',
                        cache:false,
                        data:JSON.stringify(tmp),
                        dataType:'JSON',
                        contentType:'application/json;charset=utf-8',
                        success:function(data){
                            if(data.success){
                                _this.common.successTip("操作成功！");
                                if(biddingStatus==20){
                                    _this.$router.push("/QuotationSheet");
                                }
                            }else{
                                _this.common.errorTip(data.errorMessage);
                            }
                        }
                    })
                }else{
                    _this.common.errorTip("待报价明细不能为空！");
                    return;
                }
            },
            //含税不含税验证
            validTax(){
                var _this=this;
                if(_this.biddingDetailsData==null){
                    _this.biddingDetailsData=[];
                }
                var arr=_this.biddingDetailsData;
                //校验数字
                for(var i=0;i<arr.length;i++){
                    if(arr[i].quantityAvailable==null || arr[i].quantityAvailable==""){
                        _this.common.errorTip("可供数量不能为空");
                        return false;
                    }
                    if(arr[i].moq==null || arr[i].moq==""){
                        _this.common.errorTip("MOQ不能为空");
                        return false;
                    }
                    if(arr[i].deliveryCycle==null || arr[i].deliveryCycle==""){
                        _this.common.errorTip("交货周期不能为空");
                        return false;
                    }
                    if(!(/^(\-?)(\d+)$/.test(arr[i].quantityAvailable))){
                        _this.common.errorTip("可供数量必须是数字");
                        return false;
                    }
                    if(!(/^(\-?)(\d+)$/.test(arr[i].moq))){
                        _this.common.errorTip("MOQ必须是数字");
                        return false;
                    }
                    if(arr[i].moq<=0){
                        _this.common.errorTip("MOQ必须大于0");
                        return false;
                    }
                    if(!(/^(\-?)(\d+)$/.test(arr[i].deliveryCycle))){
                        _this.common.errorTip("交货周期必须是数字");
                        return false;
                    }
                }
                if (_this.formInline.isTax == 1) {
                    //含税，税率是必须的
                    for(var i=0;i<arr.length;i++){
                        if(arr[i].taxRate!=0){
                            if(arr[i].taxRate==null || arr[i].taxRate==""){
                                _this.common.errorTip("税率不允许为空");
                                return false;
                            }
                        }else if(arr[i].includingTaxPrice==null || arr[i].includingTaxPrice==""){
                            _this.common.errorTip("含税单价不允许为空");
                            return false;
                        }else if(arr[i].quotationAmount==null || arr[i].quotationAmount==""){
                            _this.common.errorTip("不含税单价不允许为空");
                            return false;
                        }else if(!(/^\d+(\.\d+)?$/.test(arr[i].includingTaxPrice))){
                            _this.common.errorTip("含税单价必须是数字");
                            return false;
                        }else  if(!(/^\d+(\.\d+)?$/.test(arr[i].quotationAmount))){
                            _this.common.errorTip("不含税单价必须是数字");
                            return false;
                        }
                    }
                    return true;
                }else{
                    //不含税，税率可无
                    for(var i=0;i<arr.length;i++){
                        if(arr[i].quotationAmount==null || arr[i].quotationAmount==""){
                            _this.common.errorTip("不含税单价不允许为空");
                            return false;
                        }else  if(!(/^\d+(\.\d+)?$/.test(arr[i].quotationAmount))){
                            _this.common.errorTip("不含税单价必须是数字");
                            return false;
                        }
                    }
                    return true;
                }
            },
            //保存按钮
            save(biddingStatus){
                var _this = this;
                _this.$refs["formInline"].validate((valid) => {
                    if (valid) {
                        var validTaxSave=_this.validTax();
                        if(_this.formInline.quotationNumber == "" || _this.formInline.quotationNumber == null){
                            if(validTaxSave){
                                _this.addData(biddingStatus);
                                _this.biddingStatus=20;
                            }
                        }else{
                            if(validTaxSave) {
                                _this.updateData(biddingStatus);
                            }
                        }
                    } else {
                        _this.common.errorTip("存在未填写的必填项！");
                        return false;
                 }
             });

            },
            //提交按钮
            onSubmit(biddingStatus){
                var _this = this;
                var validTaxSave=_this.validTax();
                _this.$refs["formInline"].validate((valid) => {
                    if (valid) {
                        if(_this.formInline.quotationNumber == "" || _this.formInline.quotationNumber == null){
                            if(validTaxSave){
                                _this.addData(biddingStatus);
                                _this.biddingStatus=20;
                            }
                        }else{
                            if(validTaxSave){
                                _this.updateData(biddingStatus);
                            }
                        }
                    } else {
                            _this.common.errorTip("存在未填写的必填项！");
                        return false;
                    }
                });
            },
            legalPersonDropDownValue(){  //法人名称下拉列表值
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                            if(_this.legalPersonDropDown.length == 1){
                                _this.formInline.legalPersonName = _this.legalPersonDropDown[0].legalPersonName;
                                _this.formInline.legalPersonId = _this.legalPersonDropDown[0].id;
                                _this.dropdownValue();
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 选择法人 取名称给后台
            changeLegalPerson(){
                var _this = this;
                for(var i=0;i<_this.legalPersonDropDown.length;i++){
                    if(_this.formInline.legalPersonName==_this.legalPersonDropDown[i].id){
                        _this.formInline.legalPersonId=_this.legalPersonDropDown[i].id;
                        _this.formInline.legalPersonName =_this.legalPersonDropDown[i].legalPersonName;
                    }
                }
                _this.dropdownValue();
            },
            // 选择报价单位 取名称给后台
            changeSupper(){
                var _this = this;
                for(var i=0;i<_this.formInline.selectDropdown.length;i++){
                    /*if(_this.formInline.vendor==_this.formInline.selectDropdown[i].partnerCode){
                        _this.formInline.vendorCode =_this.formInline.selectDropdown[i].partnerName;
                    }*/
                    if(_this.formInline.vendorCode==_this.formInline.selectDropdown[i].partnerCode){
                        _this.formInline.vendor =_this.formInline.selectDropdown[i].partnerName;
                        _this.formInline.vendorId =_this.formInline.selectDropdown[i].id;
                    }
                }
            },
            createInquerySheetDropdownValue(){ //下拉列表的值
                var _this =this;
                var tmp = ["type","isTax","currencyCode"];
                $.ajax({
                    type:'POST',
                    url:_this.procurement_service+'procurement/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:"application/json;charset=utf-8",
                    success: function(data){
                        if(data.success){
                            _this.selectDropdown=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                });
            },
            // 下拉列表选择币别
            selectCurrencyCode(){
                var _this=this;
                for(var i=0;i<_this.selectDropdown.currencyCode.length;i++){
                    if(_this.formInline.currencyCode == _this.selectDropdown.currencyCode[i].value){
                        _this.formInline.currencyCodeWord = _this.selectDropdown.currencyCode[i].label;
                    }
                }
                if(_this.biddingDetailsData){
                    for(var i=0;i<_this.biddingDetailsData.length;i++){
                        _this.biddingDetailsData[i].currencyCode=_this.formInline.currencyCode;
                        _this.biddingDetailsData[i].currencyCodeWord=_this.formInline.currencyCodeWord;
                    }
                }                
            },
            //待报价明细 每行的删除按钮
            handleDel(index,rows){
                rows.splice(index,1);
            },
            // 待报价明细  导入按钮
            leadingInMask(){
                var _this=this;
                _this.leadIngInVisible = true;
                _this.fileList=[];
            },
            //上传之前的钩子
            beforeAvatarUpload(file) {
                const isExcel2007 = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                const isExcel2003 = file.type === 'application/vnd.ms-excel';
                if (!isExcel2007 && !isExcel2003) {
                    this.$message.error('只能上传excel文件!');
                }
                return isExcel2007 || isExcel2003 ;
            },
            //文件上传成功时的钩子
            handleAvatarSuccess(res, file){
                var _this=this;
                if(res.success){
                    for(var i=0;i<res.data.length;i++){
                        if(_this.biddingDetailsData==null){
                            _this.biddingDetailsData=[];
                        }
                        //如果选择为不含税时，导入时含税数额清空
                        if(_this.formInline.isTax==2){
                            res.data[i].includingTaxPrice=null;
                        }
                        _this.biddingDetailsData=[];  //全清全导
                        _this.biddingDetailsData.push({
                            spareCoding:res.data[i].spareCoding,
                            spareName:res.data[i].spareName,
                            inquiryQty:res.data[i].quotedQuantity,
                            quantityAvailable:res.data[i].quantityAvailable,
                            measurementUnit:res.data[i].measurementUnit,
                            quotationAmount:res.data[i].quotationAmount,
                            includingTaxPrice:res.data[i].includingTaxPrice,
                            moq:res.data[i].moq,
                            taxRate:res.data[i].taxRate,//税率
                            deliveryCycle:res.data[i].deliveryCycle,
                            currencyCode:_this.formInline.currencyCode,
                            currencyCodeWord:_this.formInline.currencyCodeWord,
                        });
                    }
                    _this.totalQuantity();   //导入后计算报价总数量
                    // 报价不含税总金额
                    _this.formInline.totalQuotedPrice=0;
                    setTimeout(function(){
                        for(var i=0;i<_this.biddingDetailsData.length;i++){
                            if(_this.biddingDetailsData[i].inquiryQty){
                                _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                            }
                        }
                        _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                    },0);
                    // 报价含税总金额
                    _this.formInline.totalQuotedTaxPrice=0;
                    setTimeout(function(){
                        for(var i=0;i<_this.biddingDetailsData.length;i++){
                            if(_this.biddingDetailsData[i].inquiryQty){
                                _this.formInline.totalQuotedTaxPrice +=_this.biddingDetailsData[i].includingTaxPrice * _this.biddingDetailsData[i].inquiryQty;
                            }
                            
                        }
                        _this.formInline.totalQuotedTaxPrice=_this.formInline.totalQuotedTaxPrice.toFixed(8);
                    },0);
                    _this.common.successTip("上传成功！");
                    _this.leadIngInVisible = false;
                }else{
                    _this.fileList=[];
                    _this.common.errorTip(res.errorMessage);
                }
            },
            handleRemove(file, fileList) {
                 console.log(file, fileList);
            },
            handlePreview(file) {
                 console.log(file);
            },
            seeInquiry(){   //查看报价单列表
                var _this =this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+'quotationList/findByCoding?requestForQuotationId='+_this.formInline.requestForQuotationId+'&&vendorCode='+_this.formInline.vendorCode+'&&spareCodes='+_this.formInline.spareCodes,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.biddingDetailsData=data.data;
                            if(_this.biddingDetailsData!=null && _this.biddingDetailsData.length>0){
                                _this.formInline.legalPersonId=data.data[0].legalPersonId;
                                _this.formInline.vendor=data.data[0].vendor;
                                _this.formInline.vendorCode=data.data[0].vendorCode;
                                _this.formInline.vendorId=data.data[0].vendorId;
                                _this.formInline.legalPersonName=data.data[0].legalPersonName;
                            }
                            _this.totalQuantity();//计算报价总数量
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //获取下拉框数据
            dropdownValue(){  //供应商下拉列表值
                var _this=this;
                $.ajax({
                    type:'GET',
                    url:_this.metadata_service+"partner/queryByLegalPerson?legalPersonId="+_this.formInline.legalPersonId,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.formInline.selectDropdown=data.data;
                            if(_this.formInline.selectDropdown.length==1){
                                _this.formInline.vendorCode=_this.formInline.selectDropdown[0].partnerCode;
                                _this.formInline.vendor=_this.formInline.selectDropdown[0].partnerName;
                            }else{
                                _this.formInline.vendorCode=null;
                                _this.formInline.vendor=null;
                            }
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //   选泽备件弹窗的分页
            handleSpareSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.sparePageSize=val;
                _this.querySpareParts();

            },
            //点击页码分页查询点击事件
            handleSpareCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.sparePageIndex=val;
                _this.querySpareParts();
            },
            //新增备件弹窗 checkbox选中
            selectSpare(index,row,event){
                var _this = this;
                if($(event.target).parent().attr("class").indexOf("is-checked")!=-1){
                    _this.selectedSpareList.push({"id":row.id,"spareName":row.name,"spareCoding":row.code,"measurementUnit":row.unit,"MOQ":row.volumeProductionMOQ});   //新增供应商弹框中，选中的供应商code
                }else{
                    _this.selectedSpareList.pop({"id":row.id,"spareName":row.name,"spareCoding":row.code,"measurementUnit":row.unit,"MOQ":row.volumeProductionMOQ});
                }
                // console.log(_this.selectedSpareList);
            },
            // 输入可供数量
            inputQuantityAvailable(index,row){
                var _this=this;
                if(typeof(row.quantityAvailable)!="number"){
                    _this.common.errorTip("可供数量只能为数字！");
                    //row.quantityAvailable="";
                }else if(row.quantityAvailable==0){
                    _this.common.errorTip("可供数量必须大于零！");
                   // row.quantityAvailable="";
                }
            },
            // 输入不含税单价金额
            inputQuotationAmount(index,row){
                var _this=this;
                if(typeof(row.quotationAmount)!="number"){
                    _this.common.errorTip("不含税单价金额只能为数字！");
                    row.quotationAmount="";
                }else if(row.quotationAmount==0){
                    _this.common.errorTip("不含税单价金额必须大于零！");
                    row.quotationAmount="";
                }

                if(row.quotationAmount != "" || row.quotationAmount != null){
                    //税率不为空时，计算含税单价
                    /*if(row.taxRate!=null){
                        row.includingTaxPrice=(row.quotationAmount*(1+row.taxRate/100)).toFixed(8);//计算含税单价金额=不含税金额＊（1+税率）
                    }*/
                    _this.formInline.totalQuotedPrice=0;
                    setTimeout(function(){
                        for(var i=0;i<_this.biddingDetailsData.length;i++){
                            if(_this.biddingDetailsData[i].inquiryQty){
                                _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                            }
                        }
                        _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                    },0);
                }else{
                    _this.common.errorTip("单价不能为空");
                }
            },
            //输入含税单价金额
            inputTaxPrice(index,row){
                var _this=this;
                if(row.includingTaxPrice != "" || row.includingTaxPrice != null){
                    // row.price = row.price.toFixed(2);
                   //row.amount=row.price * row.quantity;
                    if(row.taxRate!=null){
                        row.quotationAmount = (row.includingTaxPrice / (1 + row.taxRate / 100)).toFixed(8);//计算不含税单价金额=含税单价金额/（1+税率）
                    }
                    _this.formInline.totalQuotedTaxPrice=0;
                    _this.formInline.totalQuotedPrice=0;
                    setTimeout(function(){
                        for(var i=0;i<_this.biddingDetailsData.length;i++){
                            if(_this.biddingDetailsData[i].inquiryQty){
                                _this.formInline.totalQuotedTaxPrice +=_this.biddingDetailsData[i].includingTaxPrice * _this.biddingDetailsData[i].inquiryQty;
                                _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                            }
                        }
                        _this.formInline.totalQuotedTaxPrice=_this.formInline.totalQuotedTaxPrice.toFixed(8);
                        _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                    },0);
                }else{
                    _this.common.errorTip("单价不能为空");
                }

            },
            //选择税率
            inputTaxRate(index,row){
                var _this = this;
                if (_this.formInline.isTax == 1) {
                    //含税
                    if(row.includingTaxPrice!=null){
                        row.quotationAmount = (row.includingTaxPrice / (1 + row.taxRate / 100)).toFixed(8);//计算不含税单价金额=含税单价金额/（1+税率）
                    }
                    _this.formInline.totalQuotedPrice=0;
                    for(var i=0;i<_this.biddingDetailsData.length;i++){
                        if(_this.biddingDetailsData[i].quotationAmount){
                            _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                        }
                    }
                    _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                }

            },
            //选择是否含税，计算含税不含税单价
            updateTax(){
                var _this=this;
                if(_this.biddingDetailsData==null){
                    _this.biddingDetailsData=[];
                }
                var arr=_this.biddingDetailsData;
                _this.formInline.totalQuotedPrice=0;
                _this.formInline.totalQuotedTaxPrice=0;
                if(_this.formInline.isTax == 2){
                    for(var i=0;i<arr.length;i++){
                        //不含税
                        arr[i].includingTaxPrice=null;
                        arr[i].taxRate=null;
                        _this.taxDisabled=true;
                        if(_this.biddingDetailsData[i].quotationAmount){
                            _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                        }
                    }
                    _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                }else if(_this.formInline.isTax==1){
                    //含税
                    for(var i=0;i<arr.length;i++){
                        if(arr[i].includingTaxPrice==null){
                            arr[i].quotationAmount=null;
                        }
                        //导入后选择含税，如果含税金额和税率都不为空，则重新计算不含税金额
                        if(arr[i].includingTaxPrice!=null && arr[i].taxRate!=null){
                            arr[i].quotationAmount = (arr[i].includingTaxPrice / (1 + arr[i].taxRate / 100)).toFixed(8);//计算不含税单价金额=含税单价金额/（1+税率）
                        }
                        _this.taxDisabled=false;
                        if(_this.biddingDetailsData[i].includingTaxPrice){
                            _this.formInline.totalQuotedTaxPrice +=_this.biddingDetailsData[i].includingTaxPrice * _this.biddingDetailsData[i].inquiryQty;
                        }
                        if(_this.biddingDetailsData[i].quotationAmount){
                            _this.formInline.totalQuotedPrice +=_this.biddingDetailsData[i].quotationAmount * _this.biddingDetailsData[i].inquiryQty;
                        }
                    }
                    _this.formInline.totalQuotedTaxPrice=_this.formInline.totalQuotedTaxPrice.toFixed(8);
                    _this.formInline.totalQuotedPrice=_this.formInline.totalQuotedPrice.toFixed(8);
                }

            },
            // 交货周期（天）
            inputDeliveryCycle(index,row){
                var _this=this;
                if(typeof(row.deliveryCycle)!="number"){
                    _this.common.errorTip("交货周期（天）只能为数字！");
                    //row.deliveryCycle="";
                }else if(row.deliveryCycle==0){
                    _this.common.errorTip("交货周期（天）必须大于零！");
                    //row.deliveryCycle="";
                }
            },
           /* // 汇率
            InputExchangeRate(index,row){
                var _this=this;
                if(typeof(row.exchangeRate)!="number"){
                    _this.common.errorTip("汇率只能为数字！");
                }else if(row.exchangeRate==0){
                    _this.common.errorTip("汇率必须大于零！");
                }
            },*/
            // MOQ
            inputMOQ(index,row){
                var _this=this;
                if(typeof(row.moq)!="number"){
                    _this.common.errorTip("MOQ只能为数字！");
                  //  row.moq=1;
                }else if(row.moq==0){
                    _this.common.errorTip("MOQ必须大于零！");
                  //  row.moq=1;
                }
            },
            //   分页
            handleSupplierSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.supplierPageSize=val;
                _this.querySuppliers();
            },
            //点击页码分页查询点击事件
            handleSupplierCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.supplierPageIndex=val;
                _this.querySuppliers();
            },
            // // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }

        }
    }
</script>
<style scoped>
    .el-select .el-tag{
        margin: 0;
    }
    .el-select__tags .el-select__input {
        display: none;
    }
    .main-right .el-pagination{
        margin-top: 0;
    }
</style>